﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DataLayer;
using DataLayer.Entities;
using Moq;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;

namespace UnitTests
{
    [TestClass]
    public class when_getting_current_exchange_rate
    {
        private CurrencyData _target;
        private Mock<IDatabaseEntities> _databaseEntitiesMock;

        [TestInitialize]
        public void Initialize()
        {
            _databaseEntitiesMock = new Mock<IDatabaseEntities>();
            _target = new CurrencyData(_databaseEntitiesMock.Object);
        }

        [TestMethod]
        public void returns_exchange_rate_from_one_currency_to_another_for_the_current_date()
        {
            var data = new List<ExchangeRate>
            {
                new ExchangeRate {CountryCode = "USD", ExchangeDate = DateTime.Today.AddDays(-1), Rate = 1M }, //Yesterday's rate
                new ExchangeRate {CountryCode = "CAD", ExchangeDate = DateTime.Today.AddDays(-1), Rate = 1.0888M }, //Yesterday's rate
                new ExchangeRate {CountryCode = "USD", ExchangeDate = DateTime.Today, Rate = 1M}, //Today's rate
                new ExchangeRate {CountryCode = "CAD", ExchangeDate = DateTime.Today, Rate = 1.0861M} //Today's rate
            }.AsQueryable();

            var dbSetMock = new Mock<IDbSet<ExchangeRate>>();
            dbSetMock.Setup(x => x.Provider).Returns(data.Provider);
            dbSetMock.Setup(x => x.Expression).Returns(data.Expression);
            dbSetMock.Setup(x => x.ElementType).Returns(data.ElementType);
            dbSetMock.Setup(x => x.GetEnumerator()).Returns(data.GetEnumerator());

            _databaseEntitiesMock.Setup(x => x.ExchangeRates)
                .Returns(dbSetMock.Object);

            var result = _target.GetCurrentExchangeRate("USD", "CAD");
            Assert.AreEqual(1.0861M, result);
        }
    }
}
